package client.comm.baoding.ui

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.baoding.R
import client.comm.baoding.ui.vm.TxViewModel
import client.comm.baoding.api.bean.UserInfo
import java.util.*
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.api.event.EventUserInfo
import client.comm.baoding.databinding.ActivityApplyTxBinding
import org.greenrobot.eventbus.EventBus

class ApplyTxActivity : BaseMiddleActivity<TxViewModel, ActivityApplyTxBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_apply_tx
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initView() {
        binding?.apply {
            event = this@ApplyTxActivity
            vm = viewModel
            StoreUtil.getBean<UserInfo>(this@ApplyTxActivity)?.let {
                viewModel.userInfo.value = it
//                integral = it.integral
                income = it.income.toString()
            }

        }

    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    EventBus.getDefault().post(EventUserInfo())
                    jl2()
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })

        viewModel.codeRet.observe(this, {

            it?.apply {
                if (isSuccess) {
                    toast("获取验证码成功")

                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }

                    viewModel.getCodeEnable.postValue(false)

                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()

                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }

                } else {
                    toast(it.message)
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })

    }

    override fun initViewModel(): TxViewModel = createViewModel()
    override fun onDestroy() {

        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }

        super.onDestroy()
    }

    fun ok() {
        //|| viewModel.smscode.value.isNullOrEmpty()
        if (TextUtils.isEmpty(viewModel.txNum.value) || viewModel.pType.value!! < 0) {
            toast("所有不能为空")
            return
        }

        ValidatePwdDialog(this).apply {
            title = "需要验证支付密码"
            ok = {
                viewModel.ok(it, "4")
            }

        }.show()
    }

    fun jl2() {
        startActivity(Intent(this, TxjlActivity::class.java).apply {
            putExtra("type", 4)
        })
    }

    fun zz() {
        startActivity(Intent(this, Zz2Activity::class.java))
        finish()
    }

}